package com.zwb.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwb.boot.common.R;
import com.zwb.boot.service.EmployeeService;
import com.zwb.boot.entity.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 头发又黑又长
 * @Date 2022/7/18 14:56
 * @email zwb15083976291@163.com
 */
@Api("员工管理")
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
//    @ApiOperation(value = "员工登录接口")
//    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee) {

        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername())
                .eq(Employee::getPassword, password);

        Employee loginEmployee = employeeService.getOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(loginEmployee)) {
            return R.error("用户名或密码错误");
        }
        if (loginEmployee.getStatus() == 0) {
            return R.error("用户已禁用");
        }
        request.getSession().setAttribute("employee", loginEmployee.getId());
        return R.success(loginEmployee);
    }


    /**
     * 员工退出
     *
     * @param request
     * @return
     */
//    @ApiOperation(value = "员工退出登录接口")
//    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }


    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @ApiOperation(value = "新增员工接口")
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        // 设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Long userId = (Long) request.getSession().getAttribute("employee");
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 获取 员工信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "获取员工信息分页接口")
    @GetMapping("/page")
    public R<Page<Employee>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) String name) {
        // 创建 分页对象
        Page<Employee> employeePage = new Page<>(page, pageSize);
        // 创建条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 添加 name 查询
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name)
                // 排序添加
                .orderByDesc(Employee::getUpdateTime)
        ;
        employeeService.page(employeePage, queryWrapper);
        return R.success(employeePage);
    }

    /**
     * 修改员工信息
     *
     * @param request
     * @param employee
     * @return
     */
    @ApiOperation(value = "修改员工信息接口")
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        Long userId = (Long) request.getSession().getAttribute("employee");
        employeeService.updateById(employee);
        return R.success("更新成功");
    }


    /**
     * 获取单个员工信息
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取单个员工信息接口")
    @GetMapping("/{id}")
    public R<Employee> getEmployee(@PathVariable("id") Long userId) {
        Employee employee = employeeService.getOne(new LambdaQueryWrapper<Employee>().eq(Employee::getId, userId));
        if (ObjectUtils.isEmpty(employee)) {
            return R.error("用户不存在");
        }
        return R.success(employee);
    }

}
